package cn.mhome.merchant.server.clazz.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.mhome.merchant.dao.common.po.ClassBusiness;
import cn.mhome.merchant.dao.common.po.ClassType;
import cn.mhome.merchant.dao.jpa.clazz.ClassBusinessDao;
import cn.mhome.merchant.dao.jpa.clazz.ClassTypeDao;
import cn.mhome.merchant.entity.clazz.param.ClassTypeParam;
import cn.mhome.merchant.entity.clazz.result.ClassTypeResult;
import cn.mhome.merchant.entity.common.result.CommonResult;
import cn.mhome.merchant.entity.common.result.Pager;
import cn.mhome.merchant.enums.ClassTypeState;
import cn.mhome.merchant.util.DateUtil;

/**
 * 类别管理
 */
@Service
public class ClassTypeServiceImpl implements ClassTypeService{
	
	@Resource
	private ClassTypeDao classTypeDao;
	@Resource
	private ClassBusinessDao classBusinessDao;

	@Override
	@Transactional(readOnly=true)
	public List<ClassTypeResult> findForList(ClassTypeParam classParam) {
		return classTypeDao.findForList(classParam);
	}

	@Override
	@Transactional(readOnly=true)
	public Pager<ClassTypeResult> findForPage(ClassTypeParam classParam) {
		return classTypeDao.findForPage(classParam);
	}

	@Override
	@Transactional
	public CommonResult save(ClassTypeParam classParam) {
		CommonResult result = new CommonResult();
		try {
			Long count = 0L;
			if(classParam.getClassTypeId() > 0){
				count = classTypeDao.findSameCount(classParam.getClassTypeId(),classParam.getSuperClassId(),classParam.getClassTypeName());
			}else{
				count = classTypeDao.findSameCount(classParam.getSuperClassId(),classParam.getClassTypeName());
			}
			if(count != null && count > 0){
				result.setCode(300);
				result.setMsg("名称重复");
				return result;
			}
			ClassType classType = null;
			if(classParam.getClassTypeId() > 0){
				classType = classTypeDao.findOne(classParam.getClassTypeId());
			}else{
				classType = new ClassType();
				classType.setCreateUser(classParam.getCreateUser());
				classType.setCreateTime(DateUtil.getOperateTime());
				classType.setState(ClassTypeState.OK.getCode());
				classType.setBusinessNum(0);
			}
			classType.setClassTypeName(classParam.getClassTypeName());
			classType.setClassTypeIcon(classParam.getClassTypeIcon());
			classType.setSuperClassId(classParam.getSuperClassId());
			classType = classTypeDao.save(classType);
			result.setCode(200);
			result.setMsg("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
		return result;
	}

	@Override
	@Transactional
	public CommonResult changeState(ClassTypeParam classParam) {
		CommonResult result = new CommonResult();
		ClassType classType = classTypeDao.findOne(classParam.getClassTypeId());
		classType.setState(classParam.getState());
		classType = classTypeDao.save(classType);
		result.setCode(200);result.setMsg("设置成功");
		return result;
	}
	
	@Override
	@Transactional
	public CommonResult delete(ClassTypeParam classParam) {
		CommonResult result = new CommonResult();
		ClassType classType = classTypeDao.findOne(classParam.getClassTypeId());
		if(classType.getBusinessNum() > 0){
			result.setCode(300);
			result.setMsg("该类别下存在商家绑定不可删除");
			return result;
		}
		
		/**
		 * 删除关系
		 */
		classBusinessDao.deleteRelationByCId(classParam.getClassTypeId());
		classTypeDao.delete(classType);
		result.setCode(200);result.setMsg("删除成功");
		return result;
	}

	@Override
	@Transactional
	public CommonResult saveRelation(ClassTypeParam classParam) {
		CommonResult result = new CommonResult();
		ClassType classType = classTypeDao.findOne(classParam.getClassTypeId());
		if(classType == null){
			result.setCode(300);
			result.setMsg("类型已被删除");
			return result;
		}
		classBusinessDao.deleteRelationByCId(classParam.getClassTypeId());
		Set<ClassBusiness> cbs = new HashSet<ClassBusiness>();
		for(Long businessId : classParam.getBusinessIds()){
			ClassBusiness classBusiness = new ClassBusiness();
			classBusiness.setBuinessId(businessId);
			classBusiness.setClassId(classParam.getClassTypeId());
			classBusiness.setIndex(10);
			cbs.add(classBusiness);
		}
		classBusinessDao.save(cbs);
		result.setCode(200);result.setMsg("保存成功");
		return result;
	}

	/**
	 * 查询一类
	 */
	@Override
	public List<ClassTypeResult> findSuperClassList() {
		List<ClassTypeResult> list = new ArrayList<>();
		List<ClassType> classTypes = classTypeDao.findSuperClassList();
		for(ClassType ct : classTypes){
			ClassTypeResult ctr = new ClassTypeResult();
			ctr.setClassTypeId(String.valueOf(ct.getClassTypeId()));
			ctr.setClassTypeName(ct.getClassTypeName());
			list.add(ctr);
		}
		return list;
	}


}
